package com.zerui.manager.app.openplan.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zerui.cola.dto.PageResponse;
import com.zerui.devicemanager.provider.DeviceDubboService;
import com.zerui.devicemanager.provider.cmd.DeviceNodePageCmd;
import com.zerui.devicemanager.provider.dto.DeviceDTO;
import com.zerui.manager.devicegroup.DeviceGroup;
import com.zerui.manager.devicegroup.domainservice.DeviceGroupRepository;
import com.zerui.manager.openplan.gateway.OpenPlanGateWay;
import com.zerui.manager.web.cmd.OpenPlanDeviceQryCmd;
import com.zerui.manager.web.vo.OpenPlanDeviceVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-09-04 16:44
 */
@Component
@CatchAndLog
public class OpenPlanDeviceQryCmdExe {

    @DubboReference
    private DeviceDubboService deviceDubboService;
    @Value("${spring.application.name}")
    private String applicationName;
    @Resource
    private DeviceGroupRepository deviceGroupRepository;
    @Resource
    private OpenPlanGateWay openPlanGateWay;

    public PageResponse<OpenPlanDeviceVO> execute(OpenPlanDeviceQryCmd openPlanQryCmd) {
        DeviceNodePageCmd cmd = new DeviceNodePageCmd();
        cmd.setBizUnit(applicationName);
        cmd.setName(openPlanQryCmd.getName());
        cmd.setPageIndex(openPlanQryCmd.getPageIndex());
        cmd.setPageSize(openPlanQryCmd.getPageSize());
        PageResponse<DeviceDTO> response = deviceDubboService.getPageDevcieList(cmd);
        List<DeviceDTO> data = response.getData();
        List<OpenPlanDeviceVO> openPlanDeviceVos = new ArrayList<>(16);
        if(CollectionUtils.isNotEmpty(data)){
            Set<Long> deviceIds = data.stream().map(DeviceDTO::getDeviceId).collect(Collectors.toSet());
            Map<Long, DeviceGroup> deviceGroupMapByIds = deviceGroupRepository.getDeviceGroupMapByIds(deviceIds);
            Map<Long, Long> openPlanDevices = openPlanGateWay.getOpenPlanDeviceMapById(new ArrayList<>(deviceIds));

            data.forEach(e->{

                OpenPlanDeviceVO vo = new OpenPlanDeviceVO();
                vo.setName(e.getName());
                vo.setDeviceId(e.getDeviceId());
                vo.setPosition(e.getPosition());
                vo.setType(e.getType());
                vo.setTypeName(e.getTypeName());
                vo.setGroupName(deviceGroupMapByIds.get(e.getDeviceId()).getOrgName());
                Long openId = openPlanDevices.get(e.getDeviceId());
                if(ObjectUtils.isNotEmpty(openId)){
                    vo.setOpenPlanId(openId);
                    vo.setOpenPlanName(openPlanGateWay.selectById(openId).getName());
                }

	            openPlanDeviceVos.add(vo);
            });
        }
        return PageResponse.of(openPlanDeviceVos,response.getTotalCount(),
                 response.getPageSize(),
                 response.getPageIndex());

    }
}
